﻿class Viewer {
    public Viewer(vname:String = null) {
        super();

        viewName = vname;
    }

    public function setName(aviewName:String):void {
        viewName = aviewName
    }

    public function getName():String {
        return viewName;
    }

    // called by controller, when c.addViewer is called
    public function setController(c:Controller):void {
        controller = c;
    }

    private function getController():Controller {
        return controller;
    }

    private notifyServerDoSth(sth) {
        controller.notifyServerDoSth(sth);
    }

    // ---viewer---
    public doSth(sth) {
        // viewer execute sth
    }

    private var viewName:String;
    private var controller:Controller;
}

class Controller {
    private viewers:Array = new Array();
    private so = null;

    public function addViewer(v:Viewer):void {
        v.setController(this);
        viewers.push(v);
    }

    public function initNC(nc, channelName) {
        so = new SO(nc, channelName);
        so.add("changed", soChanged);
    }

    public notifyServerDoSth(sth) {
        // sync, server do sth
        so.setP(key, sth);
    }

    private function soChanged(e:*):void {
        sth = so.getSth();
        doSth(sth);
    }

    private doSth(sth) {
        v.doSth(sth)
    }
}



var viewerName:String = "viewerName";
var channelName:String = "channelName";
var nc:NCHandler = null;



var viewer:Viewer = new Viewer(viewerName);

// for more than one viewers one controller
var controller:Controller = new Controller();
controller.addView(viewer);
controller.initNC(nc, channelName);




